//
//  ShoppingCartListCollectionViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/6.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class ShoppingListCollectionViewCell: UICollectionViewCell, BottomShadow, AddCornerUIRectCorner {

    @IBOutlet weak var showImageView: UIImageView!
    @IBOutlet weak var commodityNameLabel: UILabel!
    @IBOutlet weak var parameterLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
    @IBOutlet weak var imageHeight: NSLayoutConstraint!
    
    var model: GoodsModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setShadow(view: self, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: self.contentView, aCorners: .allCorners, aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        self.clipsToBounds = true
        // Initialization code
    }
    
    func showData(model: GoodsModel) {
        self.model = model
        let path:String = "\(BaseURL)\(model.coverPath!)"
        
   
        if let showImage = model.coverPath {
            let path:String = "\(BaseURL)\(showImage)"
            

            let url:URL = URL(string:path)!
            showImageView.sd_setImage(with: url, completed: nil)

        }
        commodityNameLabel.text = model.goodsName!
        if let goodsDescribe = model.goodsDescribe{
            parameterLabel.text = goodsDescribe
        }
        
        if (Int(model.coverHeight!) == 0 || Int(model.coverWidth!) == 0) {
            imageHeight.constant = 240
            return
        }
        let height:CGFloat = CGFloat(Float(model.coverHeight!)! / Float(model.coverWidth!)!)
        imageHeight.constant = height * (ScreenWidth - 30) / 2
    }
    
    override func draw(_ rect: CGRect) {
        
    }
    
    static func caculateHeight(model: GoodsModel) -> CGFloat{
        if (Int(model.coverHeight!) == 0 || Int(model.coverWidth!) == 0){
            return 240 + 115
        }
        var height:CGFloat = CGFloat(Float(model.coverHeight!)! / Float(model.coverWidth!)!)
        height = height * (ScreenWidth - 30) / 2 + 115
        return height
    }
    
    
}

